package com.app114.app.android.rk;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.DownloadListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.app114.app.android.rk.bean.WebSiteItem;
import com.app114.app.android.rk.conf.Conf;
import com.app114.app.android.rk.log.Logger;
import com.app114.app.android.rk.webkit.WebviewUtils;
import com.app114.app.android.rk.widget.ExtendedWebView;

public class WebFragment extends Fragment implements OnClickListener, DownloadListener {
	private static final String TAG = WebFragment.class.getSimpleName();
	private WebSiteItem mItem;
	private ExtendedWebView mWebView;
	private String url;

	// private Animation leftIn, rightOut, refreshAnimation;

	WebFragment(WebSiteItem item, Handler handler) {
		this.mItem = item;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		final View view = inflater.inflate(R.layout.layout_web, container, false);
		mWebView = (ExtendedWebView) view.findViewById(R.id.webview);
		return view;
	}

	public String getCurrentUrl() {
		return mWebView.getUrl();
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		url = mItem.getGUrl();
		WebviewUtils.webViewSettings(mWebView);

		mWebView.setWebViewClient(new MyWebClient());
		// mWebView.setWebChromeClient(new MyClient());
		mWebView.setDownloadListener(this);
		// mWebView.setWebViewClient(new WebViewClient());

		// leftIn = AnimationUtils.makeInAnimation(mContext, true);
		// rightOut = AnimationUtils.makeOutAnimation(mContext, false);
		// refreshAnimation = AnimationUtils.loadAnimation(mContext,
		// R.anim.reload_img);
		// refreshAnimation.setRepeatMode(Animation.RESTART);
		//
		// mWebView.startAnimation(leftIn);
		Conf.currentUrl = url ;
		mWebView.loadUrl(url);
	}

	final class MyClient extends WebChromeClient {

		@Override
		public Bitmap getDefaultVideoPoster() {
			// TODO Auto-generated method stub
			return super.getDefaultVideoPoster();
		}

		@Override
		public View getVideoLoadingProgressView() {
			// TODO Auto-generated method stub
			return super.getVideoLoadingProgressView();
		}

		@Override
		public void onProgressChanged(WebView view, int progress) {

			Logger.i(TAG, "onProgressChanged :" + progress);
			// Activity和Webview根据加载程度决定进度条的进度大小
			// 当加载到100%的时候 进度条自动消失
			// setProgress(progress * 100);
		}

	}

	private class MyWebClient extends WebViewClient {

		/**
		 * 重写，过滤请求
		 */
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			Conf.currentUrl = url ;
			mWebView.loadUrl(url);
			return true;
		}

		/**
		 * 过滤url加载过程
		 */
		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			super.onPageStarted(view, url, favicon);
		}

		@Override
		public void onPageFinished(WebView view, String url) {
			super.onPageFinished(view, url);
		}

		@Override
		public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
			super.onReceivedError(view, errorCode, description, failingUrl);
			view.loadData("errorCode:" + errorCode + "\r\nfailingUrl:" + failingUrl, Conf.MIMETYPE, Conf.ENCODING);
		}
	}

	@Override
	public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
		 Uri uri = Uri.parse(url);
		 Intent intent = new Intent(Intent.ACTION_VIEW, uri);
		 startActivity(intent);
//		Intent intent = new Intent();
//		intent.setAction("android.intent.action.VIEW");    
//		intent.setClassName("com.android.browser","com.android.browser.BrowserActivity");  
//		intent.setDataAndType(Uri.parse(url), mimetype);
//		startActivity(intent);
	}

	@Override
	public void onClick(View v) {
		// Toast.makeText(getActivity(), "hello slide view layout",
		// Toast.LENGTH_SHORT).show();
	}

}
